#include <sys/types.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <list>
#include <string>
#include <string.h>
using std::list;
#define OPEN_FILE_BUFFER 4096
struct vertex_chunk
{
	float x,y,z;
};
struct face_chunk
{
	uint16_t one,two,three;
};
struct texture_chunk
{
	float s,t;
};
struct common_chunk
{
	uint16_t id;
	uint32_t length;
	uint16_t par_id;
};
class chunkData
{
public:
	virtual int get_vertex_length() const=0;
	virtual int get_face_length() const=0;
	virtual int get_texture_length() const=0;
	virtual const vertex_chunk* get_vertex()=0; //a list [(1,2,3)][(4,5,6)][(7,8,9)]
	virtual const face_chunk* get_face()=0;
	virtual const texture_chunk* get_texture()=0;
	virtual const char* get_name()=0;
protected:
	vertex_chunk *vertex;
	face_chunk *face;
	texture_chunk* texture;
	int _vertex_index;
	int _max_vertex_num;
	int _face_index;
	int _max_face_num;
	int _texture_index;
	int _max_texture_num;
	std::string name;
};

class chunk : public chunkData
{
public:
	chunk();
	~chunk();
	int get_vertex_length() const;
	int get_face_length() const;
	int get_texture_length() const;
	const vertex_chunk* get_vertex();
	const face_chunk* get_face();
	const texture_chunk* get_texture();
	const char* get_name();
	
	
	void setName(const char* name);
	void initVertex(int);
	void initFace(int);
	void initTexture(int);
	void append_vertex(vertex_chunk&);
	void append_face(face_chunk&);
	void append_texture(texture_chunk&);
};

class dsloader
{
public:
	dsloader(const char*filename);
	dsloader();
	~dsloader();
	int load(const char*filename);
	const list<chunkData*>& getListChunk();
protected:
	int fread_fd_chunk(uint16_t pre_id=0);
	int fread_vertex();
	int fread_face();
	int fread_texture();
	int fread_moduleinfo();
private:
	FILE* global_fileid;
	int level;
#ifdef OPEN_FILE_BUFFER
	char* filebuffer;
#endif
	chunk* current_chunk;
	std::list<chunkData*> list_chunk;
};
